package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.GradeMapper;
import com.example.demo.mapper.MajorsMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.entity.Grade;
import com.example.demo.entity.Majors;
import com.example.demo.entity.Message;
import com.example.demo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
public class AddStuController {
    @Autowired
    private StudentMapper sm;
    @Autowired
    private MajorsMapper majorsMapper;
    @Autowired
    private GradeMapper gradeMapper;
@RequestMapping("/addstu")
    public void updatestu(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Integer id=Integer.valueOf(request.getParameter("id"));
    String name=request.getParameter("name");
    String college=request.getParameter("college");
    String major=request.getParameter("major");
    String classi=request.getParameter("classi");
    String tele=request.getParameter("tele");
    if(Objects.nonNull(id) &&!"".equals(college)&&!"".equals(major)&&!"".equals(classi)&&!"".equals(tele)&&name!=null&&college!=null&&major!=null&&classi!=null&&tele!=null){
        Majors majors = majorsMapper.selectByCollege(college);
        if(majors==null) response.getWriter().write(JSONObject.toJSONString(Message.failure("Data error,college is not exist")));
        List<Student> list =new ArrayList<Student>();
        list=sm.findAlllStu();
        boolean isIdExist=list.stream().anyMatch(student -> Objects.equals(student.getId(), id));
        assert majors != null;
        boolean isMajorExist=majors.getMajor().contains(major);
        if(!isIdExist&&!isMajorExist){
            Date date = new Date(System.currentTimeMillis());
            String test=new SimpleDateFormat("yyyy").format(date);
            int y = Integer.parseInt(test);
            String grade=y+"级";
            Grade grade1 = gradeMapper.selectByCollegeAndGrade(college, grade);
            if(grade1==null) response.getWriter().write(JSONObject.toJSONString(Message.failure("Data error,grade is not exist")));
            Student addstu=new Student(id,name,college,major,classi,grade,tele);
            int n=sm.insertstu(addstu);
            if(n>0){
                response.getWriter().write(JSONObject.toJSONString(Message.success("Success add")));
            }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Failure add")));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("The data you input does not exist or is illegal.")));
    }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Data error")));
}
}
